//
//  HomePageVC.m
//  u76ho
//
//  Created by m-air-01 on 15/12/25.
//  Copyright © 2015年 LV. All rights reserved.
//

#import "HomePageVC.h"
#import "ScorllPictureView.h"
#import "YiQiViewController.h"
#import "TopicActivityCell.h"
//#import "HomePageScrollTableViewCell.h"
#import "TableCollectionCell.h"

#import "BottomCompanyCell.h"

#import "SearchViewController.h"
#import "HomePageModel.h"
#import "CompanyListViewController.h"
#import "ActivityViewController.h"
#import "ChengZhangViewController.h"
#import "WelcomeViewController.h"
#import "LoginViewController.h"
#import "WebViewController.h"
@interface HomePageVC ()
<UITableViewDataSource,UITableViewDelegate,ScrollPictureDelegate,UIAlertViewDelegate>
{
    UITableView * _tableView;
    ScorllPictureView * scrollView;
    
    HomePageModel * myModel;
    NSArray *  HuodongArray;//判断活动有几个数组
    
    NSMutableArray * scrolldataArr;
    NSArray * scrollViewYrlArray;
    
    NSMutableArray * BottomHuoDongArray;
}
@property(nonatomic,strong)CCEaseRefresh * Refresh;

@end
@implementation HomePageVC

//#pragma mark - 视图生命周期
//-(void)viewWillAppear:(BOOL)animated
//{
//    
//}

-(void)viewDidLoad
{
    [super viewDidLoad];
    [self main];
    
    BottomHuoDongArray = [[NSMutableArray alloc]init];
    [self addNavgationBar];
    [self addLeftItem];
    
    if ([self isLogin]) {
        NSLog(@"login");
        
//        LoginViewController * loginVC = [[LoginViewController alloc]init];
//        [self.navigationController pushViewController:loginVC animated:NO];
    }
    else
    {
        NSLog(@"no login");
    }
   
}
-(void)main
{
    scrolldataArr = [[NSMutableArray alloc]init];
    self.navigationController.navigationBar.backgroundColor = [UIColor whiteColor];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT-49) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = UIColorFromRGB(0xf1f3f3);
    _tableView.tableHeaderView = [self createTableViewHeaderView];
    [_tableView registerNib:[UINib nibWithNibName:@"TopicActivityCell" bundle:nil] forCellReuseIdentifier:@"TopicActivityCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"BottomCompanyCell" bundle:nil] forCellReuseIdentifier:@"BottomCompanyCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"TableCollectionCell" bundle:nil] forCellReuseIdentifier:@"TableCollectionCell"];
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    //[self requestData];
    
    _tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
  
    
    _Refresh = [[CCEaseRefresh alloc] initInScrollView:_tableView];
    [ _Refresh addTarget:self action:@selector(dropViewDidBeginRefreshing:) forControlEvents:UIControlEventValueChanged];
    
    // auto refresh
    [_Refresh beginRefreshing];
    
    _tableView.contentOffset = CGPointMake(0, -64);

}
-(void)dropViewDidBeginRefreshing:(CCEaseRefresh *)refreshControl{
    
    
//    [self gaode];
    
    double delayInSeconds = 1.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self requestData];
        
        
    });




}
-(void)requestData
{
//    __weak typeof(self)weadSelf = self;
    [U76HONet getHomepageDataBlock:^(HomePageModel *model) {
        NSLog(@"首页的数据获取成功了");
        NSArray * arr = model.scrollPicModelArray;
        
        scrollViewYrlArray = arr;
        
        //scrolldataArr = arr;
        NSMutableArray * scrollpicArr = [[NSMutableArray alloc]init];
        for (int i = 0; i<arr.count; i++) {
            scrollPicModel * model1 = arr[i];
            [scrollpicArr addObject:[NSString stringWithFormat:@"%@%@",U76HOURL,model1.pic]];
            
        }
        scrollView.picArr = scrollpicArr;
        [scrollView refreshData];
        myModel = model;
        
        if (BottomHuoDongArray.count!=0) {
            [BottomHuoDongArray removeAllObjects];
        }
        
        [BottomHuoDongArray addObjectsFromArray: model.commendCompanyModelArray];
        [_tableView reloadData];
      //  _tableView.contentOffset = CGPointMake(0, -64);
        
        [_Refresh endRefreshing];
    }];
}
#pragma mark - _tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0||section==1) {
        return 1;
    }

    return BottomHuoDongArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    __weak typeof(self)weakSelf = self;
    //第一区
    if (indexPath.section == 0) {
        TopicActivityCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TopicActivityCell"];
        
        //cell.backgroundColor = [UIColor redColor];
        if (myModel) {
            cell.picArr = myModel.commendActivityModelArray;
            
            HuodongArray = myModel.commendActivityModelArray;
        }
        else
        {
            cell.picArr = @[@"",@"",@"",@""];
        }
        [cell setCollectionCellSelectedBlock:^(int item) {
            NSLog(@"主题活动的collection第%i个点了",item);
            
            commendActivityModel * commendAModel = myModel.commendActivityModelArray[item];
            if ([commendAModel.url isEqualToString:@"0"]) {
                ActivityViewController * activityVC = [[ActivityViewController alloc]init];
                activityVC.infoId = commendAModel.ids;
                
                activityVC.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:activityVC animated:YES];
               // activityVC.hidesBottomBarWhenPushed = NO;
            }
            else
            {
                
//                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:commendAModel.url]];
                
                WebViewController * webVC = [[WebViewController alloc]init];
                webVC.url = commendAModel.url;
                webVC.titleStr = commendAModel.title;
                webVC.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:webVC animated:YES];
                webVC.hidesBottomBarWhenPushed = NO;
            }
            
        }];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;

    }
    //第二区
    else if(indexPath.section ==1)
    {
        
                       
            
            TableCollectionCell * cell = [tableView dequeueReusableCellWithIdentifier:@"TableCollectionCell"];
            
            //cell.backgroundColor = [UIColor redColor];
            if (myModel) {
                cell.picArr = myModel.LrModelArray;
                
                //HuodongArray = myModel.LrModelArray;
            }
        
            [cell setCollectionCellSelectedBlock:^(int item) {
                NSLog(@"主题活动的collection第%i个点了",item);
                
                LRModel * commendAModel = myModel.LrModelArray[item];
                if ([commendAModel.url isEqualToString:@"0"]) {
                    ActivityViewController * activityVC = [[ActivityViewController alloc]init];
                    activityVC.infoId = commendAModel.ids;
                    
                    activityVC.hidesBottomBarWhenPushed = YES;
                    [weakSelf.navigationController pushViewController:activityVC animated:YES];
                    // activityVC.hidesBottomBarWhenPushed = NO;
                }
                else
                {
                    
                    //                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:commendAModel.url]];
                    
                    WebViewController * webVC = [[WebViewController alloc]init];
                    webVC.url = commendAModel.url;
                    webVC.titleStr = commendAModel.title;
                    webVC.hidesBottomBarWhenPushed = YES;
                    [weakSelf.navigationController pushViewController:webVC animated:YES];
                    webVC.hidesBottomBarWhenPushed = NO;
                }
                
            }];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
      
        }
    else{
        
        
        BottomCompanyCell * cell = [tableView dequeueReusableCellWithIdentifier:@"BottomCompanyCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        if (BottomHuoDongArray.count!=0) {
            cell.HomePagecommendModel = BottomHuoDongArray[indexPath.row];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    
    
        return cell;
        
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
       NSInteger t = HuodongArray.count/4;
        
        return (t+1)*64;
        
        

    }
   else if (indexPath.section == 1) {
        
        
                if (iPhone4) {
                    
                     return  100;
                    
        
                }else  if (iPhone5) {
        
                     return  110;
                    
                } else  if (iPhone6) {
                    
                    return 130;
        
                }else{
                    return 150;
                }
      
    }
    
   else{
    return SIZETO6H(85);
   }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section ==2) {
        
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 35)];
        
        
        UIImageView * imegeView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 3, 15)];
        
        imegeView.backgroundColor = [UIColor colorWithRed:0.64 green:0.80 blue:0.11 alpha:1.00];
        imegeView.layer.masksToBounds = YES;
        imegeView.layer.cornerRadius = 2;
        
        [view addSubview:imegeView];
        
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 200, 15)];
        [view addSubview:label];
        
        label.text = @"益企推荐";
        
        label.textColor = [UIColor colorWithRed:0.41 green:0.41 blue:0.42 alpha:1.00];
        label.font = [UIFont systemFontOfSize:17];
        
        //去发现页button
        UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(view.width-60, 10, 50, 15)];
//        btn.backgroundColor = [UIColor redColor];
        
        UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 0, 10, 15)];
        imageView.image = [UIImage imageNamed:@"00o"];
        
        [btn addSubview:imageView];
        
        //[btn setImage:[UIImage imageNamed:@"00o"] forState:UIControlStateNormal];
        [view addSubview:btn];
        
        [btn addTarget:self action:@selector(GotoFind:) forControlEvents:UIControlEventTouchDown];
       // btn.backgroundColor = [UIColor redColor];
        
        
        return view;
        
        
        
    }
    
    
        return nil;
}
#pragma mark --去发现页
-(void)GotoFind:(id)sender{

    
    self.tabBarController.selectedIndex = 3;

   
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    if (indexPath.section == 2) {
        
        
        if ([self isLogin]) {
            YiQiViewController * companyVC = [[YiQiViewController alloc]init];
            
            
            commendModel * model = BottomHuoDongArray[indexPath.row];
            
            companyVC.headerString = model.headpic;
            
            companyVC.yqCode = model.code;
            companyVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController  pushViewController:companyVC animated:YES];
            // companyVC.hidesBottomBarWhenPushed = NO;
        }
        

    }

}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    
    if (section ==2) {
        return 35;
    }
       return 0.0000001;
    
    
}
-(void)screenShot
{
    UIWindow * rootWindow = [UIApplication sharedApplication].keyWindow;
    UIImage * image = [UIImage imageScreenView:rootWindow.subviews.firstObject];
    [NSUserDefaults setObject:UIImagePNGRepresentation(image) forKey:@"screenShot"];

}
-(BOOL)isLogin2
{
    if ([Singleton share].code) {
        NSLog(@"登录了");
        return YES;
        
    }
    else
    {
        //        NSLog(@"还没登录呢");
        //        LoginViewController * loginVC = [[LoginViewController alloc]init];
        //        [self.navigationController pushViewController:loginVC animated:NO];
        //        return NO;
        if (SYSTEM_VERSION >= 8.0) {
            
            UIAlertController * alerVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还没有登录是否登录" preferredStyle:UIAlertControllerStyleAlert];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                LoginViewController * loginVC = [[LoginViewController alloc]init];
                
                UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:loginVC];
                nav.navigationBarHidden = YES;
                
                //[self.navigationController pushViewController:nav animated:YES];
                [self presentViewController:nav animated:YES completion:nil];
                
            }]];
            [alerVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }]];
            [self.view.window.rootViewController presentViewController:alerVC animated:YES completion:nil];
            
        }
        else
        {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您还没有登录是否登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
        return NO;
        
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    int i = (int)buttonIndex;
    if (i == 0) {
        
    }
    else if(i == 1)
    {

        NSLog(@"还没登录呢");
        LoginViewController * loginVC = [[LoginViewController alloc]init];
        loginVC.isPopVC = YES;
        
        UINavigationController * LOginNAV = [[UINavigationController alloc]initWithRootViewController:loginVC];
        LOginNAV.navigationBarHidden = YES;
        
        
        
        //
        [self presentViewController:LOginNAV animated:YES completion:nil];

    
    }
}
-(BOOL)isLogin
{
    if ([Singleton share].code) {
        NSLog(@"登录了");
        return YES;
        
    }
    else
    {
        NSLog(@"还没登录呢");
        LoginViewController * loginVC = [[LoginViewController alloc]init];
        loginVC.isPopVC = YES;
        
        UINavigationController * LOginNAV = [[UINavigationController alloc]initWithRootViewController:loginVC];
        [LOginNAV setNavigationBarHidden:YES animated:YES];
         [self presentViewController:LOginNAV animated:YES completion:nil];

        return NO;
    }
}


#pragma mark - createTableViewHeaderView
-(UIView *)createTableViewHeaderView
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,0)];
    
    if (iPhone4||iPhone5) {
        view.height = 150;

    }else if (iPhone6){
        view.height =170;
    
    }else{
    
        view.height = 200;
    }
    
    view.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.00];
    
    
    if (scrollView) {
        return scrollView;
    }
    scrollView = [[ScorllPictureView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,view.height )];
    
    
    scrollView.delegate = self;
    //scrollView.picArr = @[@"在职公司－默认动态图.png",@"在职公司－默认动态图.png",@"在职公司－默认动态图.png"];
    
    [view addSubview:scrollView];
    //[scrollView refreshData];
    
//    CGFloat width = (SCREEN_WIDTH - 40)/3;
//    
//    
//    for (int i =0; i<3; i++) {
//        
//        UIImageView * backView  = [[UIImageView alloc]initWithFrame:CGRectMake(10+(width+10)*i,130 , width, 50)];
//        backView.backgroundColor = [UIColor whiteColor];
//        
//        [view addSubview:backView];
//        
//        
//        
//        UIImageView * leftImageView  = [[UIImageView alloc]initWithFrame:CGRectMake(10, 15, (backView.width-20)/2, 20)];
//        
//        leftImageView.backgroundColor = [UIColor greenColor];
//        
//        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(60, 15, (backView.width-20)/2, 20)];
//        label.backgroundColor = [UIColor orangeColor];
//        
//        [backView addSubview:leftImageView];
//        
//        [backView addSubview:label];
//        
//        
//        
//    }
   return view;
}
#pragma mark scrollPictureDelegate
-(void)scrollPictureSelectedAt:(int)i
{
    
    
    
//    if (i<=scrollViewYrlArray.count) {
//        scrollPicModel * model1 = scrollViewYrlArray[i-1];
//        WebViewController * webVC = [[WebViewController alloc]init];
//        webVC.url = model1.url;
//        //webVC.titleStr = commendAModel.title;
//        webVC.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:webVC animated:YES];
//        webVC.hidesBottomBarWhenPushed = NO;
//    }
//    
    
    NSLog(@"主页的%i",i);
    
}
#pragma mark - addLeftItem
-(void)addLeftItem
{
    UIButton * searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [searchBtn setImage:[UIImage imageNamed:@"00u"] forState:UIControlStateNormal];
   // [searchBtn setTitle:@"益企" forState:UIControlStateNormal];
    searchBtn.frame = CGRectMake(SIZETO6W(15), 0, 40, 20);
    [searchBtn setTitleColor:UIColorFromRGB(0x5FC035) forState:UIControlStateNormal];
    searchBtn.titleLabel.font = [UIFont systemFontOfSize:20];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc]initWithCustomView:searchBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
}
#pragma mark - NAVgationBarTitle
-(void)addNavgationBar
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 35)];
    view.backgroundColor = UIColorFromRGB(0xf1f3f2);
    view.layer.masksToBounds = YES;
    view.layer.cornerRadius = 18;
    self.navigationItem.titleView = view;
    
    
    UIView * subView = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/4*3/2-300/2, 0, 300, 35)];
    subView.backgroundColor = UIColorFromRGB(0xf1f3f2);
    [view addSubview:subView];
    
    UIImageView * imgV = [[UIImageView alloc]initWithFrame:CGRectMake(SIZETO6W(15), (35-20)/2, 20, 20)];
    imgV.image = [UIImage imageNamed:@"00g"];
    [subView addSubview:imgV];
    
    UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(imgV.right + SIZETO6W(10), 0, view.frame.size.width-SIZETO6W(25)-20, 35)];
    lab.text = @"输入公司名称或职位";
    lab.textColor = UIColorFromRGB(0xBBBFC3);
    [subView addSubview:lab];
    
    subView.center = view.center;
    
    UITapGestureRecognizer * searchTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(searchTapAction:)];
    searchTap.numberOfTapsRequired = 1;
    searchTap.numberOfTouchesRequired = 1;
    [view addGestureRecognizer:searchTap];
    
}
-(void)searchTapAction:(UITapGestureRecognizer *)tap
{
    SearchViewController * searchVC = [[SearchViewController alloc]init];
    searchVC.navigationItem.hidesBackButton = YES;
    
    searchVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:searchVC animated:YES];
   // searchVC.hidesBottomBarWhenPushed = NO;
}
@end
